#include "mcursorsconfig.h"
#include "mcursorsplugin.h"

#include <KLocale>
#include <KPluginFactory>
#include <KPluginLoader>
#include <KLineEdit>
#include <KConfigGroup>

MCursorsConfig::MCursorsConfig(QWidget *parent, const QVariantList &args)
: KCModule(MCursorsPluginFactory::componentData(), parent, args)
, m_layout()
, m_underline_frame()
, m_underline_color_hlayout()
, m_underline_style_hlayout()
, m_bg_color_hlayout()
, m_underline_style()
, m_underline_color()
, m_underline_color_label(i18n("Underline color"))
, m_underline_style_label(i18n("Underline style"))
, m_bg_color_label(i18n("Background color cursor"))
, m_cursor_color()
{
	m_cursor_color.setAlphaChannelEnabled(true);
	m_bg_color_hlayout.addWidget(&m_bg_color_label);
	m_bg_color_hlayout.addWidget(&m_cursor_color);
	m_layout.addLayout(&m_bg_color_hlayout);

	m_underline_style.addItem(i18n("None"));
	m_underline_style.addItem(i18n("Single"));
	m_underline_style.addItem(i18n("Dash"));
	m_underline_style.addItem(i18n("Dot line"));
	m_underline_style.addItem(i18n("Dash dot line"));
	m_underline_style.addItem(i18n("Dash dot dot line"));
	m_underline_style_hlayout.addWidget(&m_underline_style_label);
	m_underline_style_hlayout.addWidget(&m_underline_style);
	m_layout.addLayout(&m_underline_style_hlayout);

	m_underline_color_hlayout.addWidget(&m_underline_color_label);
	m_underline_color_hlayout.addWidget(&m_underline_color);
	m_underline_frame.setLayout(&m_underline_color_hlayout);
	m_layout.addWidget(&m_underline_frame);

	setLayout(&m_layout);

	QObject::connect(&m_cursor_color, SIGNAL(changed(QColor)), this, SLOT(slotChanged()));
	QObject::connect(&m_underline_color, SIGNAL(changed(QColor)), this, SLOT(slotChanged()));
	QObject::connect(&m_underline_style, SIGNAL(currentIndexChanged(int)), this, SLOT(slotChanged()));

	QObject::connect(&m_underline_style, SIGNAL(currentIndexChanged(int)), this, SLOT(underlineStyleindexChanged(int)));
}

MCursorsConfig::~MCursorsConfig()
{
}

void MCursorsConfig::underlineStyleindexChanged(int index)
{
	m_underline_frame.setEnabled(index);
}

void MCursorsConfig::save()
{
	if (MCursorsPlugin::self())
	{
		MCursorsPlugin::self()->setCursorColor(m_cursor_color.color());
		int index = m_underline_style.currentIndex();
		if (index)
		{
			MCursorsPlugin::self()->setUnderlineStyle(getUnderlineStyle(index));
			MCursorsPlugin::self()->setUnderlineColor(m_underline_color.color());
		}
		else
			MCursorsPlugin::self()->setUnderlineStyle(QTextCharFormat::NoUnderline);
		MCursorsPlugin::self()->writeConfig();
	}
	else
	{
		KConfigGroup cg(KGlobal::config(), "MCursors Plugin");
		cg.writeEntry("cursor_color", m_cursor_color.color());
		cg.writeEntry("underline_style", m_underline_style.currentIndex());
		cg.writeEntry("underline_color", m_underline_color.text());
	}
	emit changed(false);
}

void MCursorsConfig::load()
{
	if (MCursorsPlugin::self())
	{
		MCursorsPlugin::self()->readConfig();
		m_cursor_color.setColor(MCursorsPlugin::self()->cursorColor());
		m_underline_color.setColor(MCursorsPlugin::self()->underlineColor());
		m_underline_style.setCurrentIndex(MCursorsPlugin::self()->underlineStyle());
	}
	else
	{
		KConfigGroup cg(KGlobal::config(), "MCursors Plugin");
		const DefaultValues values;
		m_cursor_color.setColor(cg.readEntry("cursor_color", values.cursorColor));
		m_underline_color.setColor(cg.readEntry("underline_color", values.underlineColor));
		m_underline_style.setCurrentIndex(cg.readEntry("underline_style", values.underlineStyle));
	}
	emit changed(false);
}

void MCursorsConfig::defaults()
{
	const DefaultValues values;
	m_cursor_color.setColor(values.cursorColor);
	m_underline_style.setCurrentIndex(values.underlineStyle);
	m_underline_color.setColor(values.underlineColor);
	emit changed(true);
}

void MCursorsConfig::slotChanged()
{
	emit changed(true);
}

#include "mcursorsconfig.moc"